package com.briup.cms.service;

import com.briup.cms.bean.BaseConfig;
import com.briup.cms.dao.ConfigMapper;
import com.briup.cms.exception.ConfigException;
import com.briup.cms.exception.CustomerException;
import com.briup.cms.service.IBaseConfigService;
import com.briup.cms.utils.MessageStatus;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BaseConfigServiceImpl implements IBaseConfigService {

    @Autowired
    private ConfigMapper mapper;

    @Override
    public void saveConfig(BaseConfig baseConfig) throws CustomerException {
        //添加一个需要被禁用，系统配置只能有一个启动
        baseConfig.setConfigStatus(0);
        mapper.insert(baseConfig);
    }

    @Override
    public void updateConfig(BaseConfig baseConfig) throws CustomerException {
        //判断配置信息是否存在
        if (baseConfig == null || baseConfig.getConfigId() == null)
            //配置id不存在
            throw new CustomerException(MessageStatus.SPECIFIED_QUESTIONED_CONFIG_ID_NOT_NULL);
        if (mapper.selectById(baseConfig.getConfigId()) == null)
            //配置信息不存在
            throw new CustomerException(MessageStatus.SPECIFIED_QUESTIONED_CONFIG_NOT_EXIST);

        mapper.update(baseConfig);

    }

    @Override
    public void changeConfigStatus(int id, int status) throws CustomerException {

        //通过id查询配置信息
        BaseConfig config = mapper.selectById(id);
        //判断配置是否存在
        if (config == null)
            throw new CustomerException(MessageStatus.SPECIFIED_QUESTIONED_CONFIG_NOT_EXIST);
        //首先把所查询出来的配置信息的状态设置为禁用状态，
        //启用一个就必须禁用一个，只能有一个系统配置
        if (config.getConfigStatus() == 1)
            throw new CustomerException(MessageStatus.SPECIFIED_QUESTIONED_CONFIG_ERROR);
        //如果已经有系统配置启用，再想启用其他的则直接设为0-禁用，实际上就是不变
        mapper.changeConfigStatusToNo();
        //将查找到的系统配置状态设为1
        config.setConfigStatus(1);
        //更新配置
        mapper.updateStatus(config);

    }

    @Override
    public void deleteById(int id) throws CustomerException {
        //通过id查询配置信息
        BaseConfig config = mapper.selectById(id);
        //判断配置是否存在
        if (config == null)
            throw new CustomerException(MessageStatus.SPECIFIED_QUESTIONED_CONFIG_NOT_EXIST);
        mapper.deleteById(id);

    }

    @Override
    public PageInfo<BaseConfig> findAllWithPage(int pageNum, int pageSize) throws CustomerException {

        PageHelper.startPage(pageNum, pageSize, true);
        List<BaseConfig> list = mapper.pageQueryAll();
        return new PageInfo(list);

    }

    @Override
    public BaseConfig currentConfigYes() throws CustomerException {

        List<BaseConfig> list = mapper.findConfigByStatus(1);
        //判断当前配置是否有信息，
        //对查询到的配置信息进行初始化，然后新建在一个新的BaseConfig中把配置信息显示出来
        if (list != null && list.size() > 0)
            return list.get(0);
        return new BaseConfig();
    }
}
